{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from collections import Counter\n",
    "import paddlehub as hub\n",
    "import paddle\n",
    "from sklearn.model_selection import train_test_split\n",
    "from paddlehub.datasets.base_nlp_dataset import TextClassificationDataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df=pd.read_excel('./data/moods_classify8_unprocessed.xlsx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.head(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.isnull().any()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df[df.isnull().values==True]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.dropna(subset=['text','label'],axis=0,how='any',inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.isnull().any()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df[df.duplicated('text')]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.drop_duplicates(subset='text',keep='first',inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.duplicated('text').any()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.boxplot(x=df.label,\n",
    "           whis=1.5,\n",
    "           widths=0.8,\n",
    "           patch_artist=True,\n",
    "           showmeans=True,\n",
    "           boxprops={'facecolor':'steelblue'},\n",
    "           flierprops={'markerfacecolor':'red','markeredgecolor':'red','markersize':4},\n",
    "           meanprops={'marker':'D','markerfacecolor':'black','markersize':4},\n",
    "           medianprops={'linestyle':'__','color':'orange'},\n",
    "           labels=[''])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Q1=df.label.quantile(q=0.25)\n",
    "Q3=df.label.quantile(q=0.75)\n",
    "low_whisker=Q1-1.5*(Q3-Q1)\n",
    "up_whisker=Q3+1.5*(Q3-Q1)\n",
    "df2=df.label[(df.label>up_whisker)|(df.label<low_whisker)]\n",
    "print(Counter(df2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df[df['label']==9.0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.drop((df[df['label']==9.0]).index,inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "(df['label']==9.0).any()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df['text'].str.len().describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_labeld=df[['label','text']]\n",
    "train,test=train_test_split(train_label,test_size=0.2,random_state=2021)\n",
    "train.to_csv('train.txt',index=False,header=False,sep='\\t')\n",
    "test.to_csv('train.txt',index=False,header=False,sep='\\t')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "txt_list=['train.txt','test.txt']\n",
    "l=0\n",
    "for file in txt_list:\n",
    "    with open(file,'r')as f:\n",
    "        l+=len(f.readloines())\n",
    "        print(\"拆分后的数据量为:\",l)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from paddlehub.datasets.base_nip_dataset import TextClassificationDataset\n",
    "class MyDataset(TextClassificationDataset):\n",
    "    base_path='data'\n",
    "    label_list=['0.0','1.0','2.0','3.0','4.0','5.0','6.0','7.0']\n",
    "    def__init__(self,tokenizer,max_seq_len:int=128,mode:str='train'):\n",
    "        if mode=='train':\n",
    "            data_file='train.txt'\n",
    "        elif mode=='test':\n",
    "            data_file='test.txt'\n",
    "        else:\n",
    "            data_file='dev.txt'\n",
    "        super()init(\n",
    "            base_path=self.base_path,\n",
    "            tokenizer=tokenizer,\n",
    "            max_seq_len=max_seq_len,\n",
    "            mode=mode,\n",
    "            data_file=data_file,\n",
    "            label_list=self.label_list,\n",
    "            is_file_with_header=False)\n",
    "model=hub.Module(name='ernle_tiny',task='seq-cis',num_classes=len(MyDataset.label_list))\n",
    "tokenizer=model.get_tokenizer()\n",
    "train_dataset=MyDataset(tokenizer)\n",
    "test_dataset=MyDataset(tokenizer,mode='test')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
